<div class="container">

        <h1 id="pkg-overview">package syntax</h1>

        <p><code>import "regexp/syntax"</code>

        </p><p>
Package syntax parses regular expressions into parse trees and compiles
parse trees into programs. Most clients of regular expressions will use the
facilities of package <a href="/regexp">regexp</a> (such as Compile and Match) instead of this package.
</p>
<h3 id="hdr-Syntax">Syntax </h3>
<p>
The regular expression syntax understood by this package when parsing with the Perl flag is as follows.
Parts of the syntax can be disabled by passing alternate flags to Parse.
</p>
<p>
Single characters:
</p>
<pre>.              any character, possibly including newline (flag s=true)
[xyz]          character class
[^xyz]         negated character class
\d             Perl character class
\D             negated Perl character class
[[:alpha:]]    ASCII character class
[[:^alpha:]]   negated ASCII character class
\pN            Unicode character class (one-letter name)
\p{Greek}      Unicode character class
\PN            negated Unicode character class (one-letter name)
\P{Greek}      negated Unicode character class
</pre>
<p>
Composites:
</p>
<pre>xy             x followed by y
x|y            x or y (prefer x)
</pre>
<p>
Repetitions:
</p>
<pre>x*             zero or more x, prefer more
x+             one or more x, prefer more
x?             zero or one x, prefer one
x{n,m}         n or n+1 or ... or m x, prefer more
x{n,}          n or more x, prefer more
x{n}           exactly n x
x*?            zero or more x, prefer fewer
x+?            one or more x, prefer fewer
x??            zero or one x, prefer zero
x{n,m}?        n or n+1 or ... or m x, prefer fewer
x{n,}?         n or more x, prefer fewer
x{n}?          exactly n x
</pre>
<p>
Implementation restriction: The counting forms x{n,m}, x{n,}, and x{n}
reject forms that create a minimum or maximum repetition count above 1000.
Unlimited repetitions are not subject to this restriction.
</p>
<p>
Grouping:
</p>
<pre>(re)           numbered capturing group (submatch)
(?P&lt;name&gt;re)   named &amp; numbered capturing group (submatch)
(?:re)         non-capturing group
(?flags)       set flags within current group; non-capturing
(?flags:re)    set flags during re; non-capturing

Flag syntax is xyz (set) or -xyz (clear) or xy-z (set xy, clear z). The flags are:

i              case-insensitive (default false)
m              multi-line mode: ^ and $ match begin/end line in addition to begin/end text (default false)
s              let . match \n (default false)
U              ungreedy: swap meaning of x* and x*?, x+ and x+?, etc (default false)
</pre>
<p>
Empty strings:
</p>
<pre>^              at beginning of text or line (flag m=true)
$              at end of text (like \z not Perl's \Z) or line (flag m=true)
\A             at beginning of text
\b             at ASCII word boundary (\w on one side and \W, \A, or \z on the other)
\B             not at ASCII word boundary
\z             at end of text
</pre>
<p>
Escape sequences:
</p>
<pre>\a             bell (== \007)
\f             form feed (== \014)
\t             horizontal tab (== \011)
\n             newline (== \012)
\r             carriage return (== \015)
\v             vertical tab character (== \013)
\*             literal *, for any punctuation character *
\123           octal character code (up to three digits)
\x7F           hex character code (exactly two digits)
\x{10FFFF}     hex character code
\Q...\E        literal text ... even if ... has punctuation
</pre>
<p>
Character class elements:
</p>
<pre>x              single character
A-Z            character range (inclusive)
\d             Perl character class
[:foo:]        ASCII character class foo
\p{Foo}        Unicode character class Foo
\pF            Unicode character class F (one-letter name)
</pre>
<p>
Named character classes as character class elements:
</p>
<pre>[\d]           digits (== \d)
[^\d]          not digits (== \D)
[\D]           not digits (== \D)
[^\D]          not not digits (== \d)
[[:name:]]     named ASCII class inside character class (== [:name:])
[^[:name:]]    named ASCII class inside negated character class (== [:^name:])
[\p{Name}]     named Unicode property inside character class (== \p{Name})
[^\p{Name}]    named Unicode property inside negated character class (== \P{Name})
</pre>
<p>
Perl character classes (all ASCII-only):
</p>
<pre>\d             digits (== [0-9])
\D             not digits (== [^0-9])
\s             whitespace (== [\t\n\f\r ])
\S             not whitespace (== [^\t\n\f\r ])
\w             word characters (== [0-9A-Za-z_])
\W             not word characters (== [^0-9A-Za-z_])
</pre>
<p>
ASCII character classes:
</p>
<pre>[[:alnum:]]    alphanumeric (== [0-9A-Za-z])
[[:alpha:]]    alphabetic (== [A-Za-z])
[[:ascii:]]    ASCII (== [\x00-\x7F])
[[:blank:]]    blank (== [\t ])
[[:cntrl:]]    control (== [\x00-\x1F\x7F])
[[:digit:]]    digits (== [0-9])
[[:graph:]]    graphical (== [!-~] == [A-Za-z0-9!"#$%&amp;'()*+,\-./:;&lt;=&gt;?@[\\\]^_`{|}~])
[[:lower:]]    lower case (== [a-z])
[[:print:]]    printable (== [ -~] == [ [:graph:]])
[[:punct:]]    punctuation (== [!-/:-@[-`{-~])
[[:space:]]    whitespace (== [\t\n\v\f\r ])
[[:upper:]]    upper case (== [A-Z])
[[:word:]]     word characters (== [0-9A-Za-z_])
[[:xdigit:]]   hex digit (== [0-9A-Fa-f])</pre>


        
  


        
        

        

        

        
        
          
        

        
        


<p><a href="https://github.com/golang/go/blob/master/src/regexp/syntax/compile.go">compile.go</a> <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/doc.go">doc.go</a> <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go">parse.go</a> <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/perl_groups.go">perl_groups.go</a> <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go">prog.go</a> <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go">regexp.go</a> <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/simplify.go">simplify.go</a> </p>

        
        

        
        

        
        
        
          <h2 data-kind="f" id="IsWordChar">func <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L107" title="View Source">IsWordChar</a>  </h2>
          <p>
IsWordChar reports whether r is consider a “word character”
during the evaluation of the \b and \B zero-width assertions.
These assertions are ASCII-only: the word characters are [A-Za-z0-9_].
</p>

          
  

        

        
        

        
          <h2 data-kind="t" id="EmptyOp">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L62" title="View Source">EmptyOp</a>  </h2>
          <p>
An EmptyOp specifies a kind or mixture of zero-width assertions.
</p>

          
          
          
  


          
            <h3 data-kind="f" id="EmptyOpContext">func <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L79" title="View Source">EmptyOpContext</a>  </h3>
            <p>
EmptyOpContext returns the zero-width assertions
satisfied at the position between the runes r1 and r2.
Passing r1 == -1 indicates that the position is
at the beginning of the text.
Passing r2 == -1 indicates that the position is
at the end of the text.
</p>

            
  

          

          
        
          <h2 data-kind="t" id="Error">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go#L16" title="View Source">Error</a>  </h2>
          <p>
An Error describes a failure to parse a regular expression
and gives the offending expression.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="Error.Error">func (*Error) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go#L21" title="View Source">Error</a>  </h3>
            
            
  

          
        
          <h2 data-kind="t" id="ErrorCode">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go#L26" title="View Source">ErrorCode</a>  </h2>
          <p>
An ErrorCode describes a failure to parse a regular expression.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="ErrorCode.String">func (ErrorCode) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go#L48" title="View Source">String</a>  </h3>
            
            
  

          
        
          <h2 data-kind="t" id="Flags">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go#L53" title="View Source">Flags</a>  </h2>
          <p>
Flags control the behavior of the parser and record information about regexp context.
</p>

          
          
          
  


          

          
        
          <h2 data-kind="t" id="Inst">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L112" title="View Source">Inst</a>  </h2>
          <p>
An Inst is a single instruction in a regular expression program.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="Inst.MatchEmptyWidth">func (*Inst) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L262" title="View Source">MatchEmptyWidth</a>  </h3>
            <p>
MatchEmptyWidth reports whether the instruction matches
an empty string between the runes before and after.
It should only be called when i.Op == InstEmptyWidth.
</p>

            
  

          
            <h3 data-kind="m" id="Inst.MatchRune">func (*Inst) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L194" title="View Source">MatchRune</a>  </h3>
            <p>
MatchRune reports whether the instruction matches (and consumes) r.
It should only be called when i.Op == InstRune.
</p>

            
  

          
            <h3 data-kind="m" id="Inst.MatchRunePos">func (*Inst) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L203" title="View Source">MatchRunePos</a>  </h3>
            <p>
MatchRunePos checks whether the instruction matches (and consumes) r.
If so, MatchRunePos returns the index of the matching rune pair
(or, when len(i.Rune) == 1, rune singleton).
If not, MatchRunePos returns -1.
MatchRunePos should only be called when i.Op == InstRune.
</p>

            
  

          
            <h3 data-kind="m" id="Inst.String">func (*Inst) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L280" title="View Source">String</a>  </h3>
            
            
  

          
        
          <h2 data-kind="t" id="InstOp">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L24" title="View Source">InstOp</a>  </h2>
          <p>
An InstOp is an instruction opcode.
</p>

          
          
          
  


          

          
            <h3 data-kind="m" id="InstOp.String">func (InstOp) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L54" title="View Source">String</a>  </h3>
            
            
  

          
        
          <h2 data-kind="t" id="Op">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go#L31" title="View Source">Op</a>  </h2>
          <p>
An Op is a single regular expression operator.
</p>

          
          
          
  


          

          
        
          <h2 data-kind="t" id="Prog">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L17" title="View Source">Prog</a>  </h2>
          <p>
A Prog is a compiled regular expression program.
</p>

          
          
          
  


          
            <h3 data-kind="f" id="Compile">func <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/compile.go#L80" title="View Source">Compile</a>  </h3>
            <p>
Compile compiles the regexp into a program to be executed.
The regexp should have been simplified already (returned from re.Simplify).
</p>

            
  

          

          
            <h3 data-kind="m" id="Prog.Prefix">func (*Prog) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L149" title="View Source">Prefix</a>  </h3>
            <p>
Prefix returns a literal string that all matches for the
regexp must start with. Complete is true if the prefix
is the entire match.
</p>

            
  

          
            <h3 data-kind="m" id="Prog.StartCond">func (*Prog) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L168" title="View Source">StartCond</a>  </h3>
            <p>
StartCond returns the leading empty-width conditions that must
be true in any match. It returns ^EmptyOp(0) if no matches are possible.
</p>

            
  

          
            <h3 data-kind="m" id="Prog.String">func (*Prog) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/prog.go#L119" title="View Source">String</a>  </h3>
            
            
  

          
        
          <h2 data-kind="t" id="Regexp">type <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go#L18" title="View Source">Regexp</a>  </h2>
          <p>
A Regexp is a node in a regular expression syntax tree.
</p>

          
          
          
  


          
            <h3 data-kind="f" id="Parse">func <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/parse.go#L701" title="View Source">Parse</a>  </h3>
            <p>
Parse parses a regular expression string s, controlled by the specified
Flags, and returns a regular expression parse tree. The syntax is
described in the top-level comment.
</p>

            
  

          

          
            <h3 data-kind="m" id="Regexp.CapNames">func (*Regexp) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go#L306" title="View Source">CapNames</a>  </h3>
            <p>
CapNames walks the regexp to find the names of capturing groups.
</p>

            
  

          
            <h3 data-kind="m" id="Regexp.Equal">func (*Regexp) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go#L62" title="View Source">Equal</a>  </h3>
            <p>
Equal returns true if x and y have identical structure.
</p>

            
  

          
            <h3 data-kind="m" id="Regexp.MaxCap">func (*Regexp) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go#L292" title="View Source">MaxCap</a>  </h3>
            <p>
MaxCap walks the regexp to find the maximum capture index.
</p>

            
  

          
            <h3 data-kind="m" id="Regexp.Simplify">func (*Regexp) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/simplify.go#L14" title="View Source">Simplify</a>  </h3>
            <p>
Simplify returns a regexp equivalent to re but without counted repetitions
and with various other simplifications, such as rewriting /(?:a+)+/ to /a+/.
The resulting regexp will execute correctly but its string representation
will not produce the same parse tree, because capturing parentheses
may have been duplicated or removed. For example, the simplified form
for /(x){1,2}/ is /(x)(x)?/ but both parentheses capture as $1.
The returned regexp may share structure with or be the original.
</p>

            
  

          
            <h3 data-kind="m" id="Regexp.String">func (*Regexp) <a href="https://github.com/golang/go/blob/master/src/regexp/syntax/regexp.go#L245" title="View Source">String</a>  </h3>
            
            
  
<div class="clearfix" id="x-footer">
  <div class="container">
    <a href="http://studygolang.com/" target="_blank">Go语言中文网</a>
    <span class="text-muted">|</span> <a href="http://golang.org/" target="_blank">Go Language</a>
    <span class="pull-right"><a href="#">Back to top</a></span>
  </div>
</div>
<script src="../assets/jquery-2.0.3.min.js"></script>
<script src="../assets/bootstrap.min.js"></script>
<script src="../assets/site.js"></script>


</div>